package com.driver.fordriver.fragment;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.driver.fordriver.R;
import com.driver.fordriver.util.XywUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import solid.ren.skinlibrary.SkinLoaderListener;
import solid.ren.skinlibrary.loader.SkinManager;


/**
 * Created by XuYeWen on 2017/4/27.
 */

public class SettingFragment extends BaseFragment {

    private RecyclerView mRecyclerView;
    private View view;
    private MyAdapter mAdapter;
    private String[] mDatas;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
         view = inflater.inflate(R.layout.fragment_setting_layout, container, false);
        init();
        return view;
    }

    @Override
    protected void init() {
        mDatas=getResources().getStringArray(R.array.item_setting);
        mRecyclerView= (RecyclerView) view.findViewById(R.id.recycleview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
        mRecyclerView.setAdapter(mAdapter = new MyAdapter());
    }

    private void onSkinSetClick() {

        File skin = new File("/mnt/sdcard/goldtheme.skin");

        if(skin == null || !skin.exists()){
            Toast.makeText(getActivity(), "请检查"  + "是否存在", Toast.LENGTH_SHORT).show();
            return;
        }

        SkinManager.getInstance().loadSkin("goldtheme.skin",
                new SkinLoaderListener() {
                    @Override
                    public void onStart() {
                        XywUtil.showToast(getActivity(),"正在切换中");
                    }

                    @Override
                    public void onSuccess() {
                        XywUtil.showToast(getActivity(),"切换成功");
                    }

                    @Override
                    public void onFailed(String errMsg) {
                        XywUtil.showToast(getActivity(),"切换失败");
                    }

                    @Override
                    public void onProgress(int progress) {

                    }

                }

        );
    }
    class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            final MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                    getContext()).inflate(R.layout.items_setting, parent,
                    false));
            return holder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.tv.setText(mDatas[position]);
            if(mDatas[position].equals(mDatas[4])){
                holder.tv.setBackgroundResource(R.drawable.button_shape_2);
            }
        }

        @Override
        public int getItemCount() {
            return mDatas.length;
        }

        class MyViewHolder extends RecyclerView.ViewHolder
        {

            Button tv;

            public MyViewHolder(View view)
            {
                super(view);
                tv = (Button) view.findViewById(R.id.id_num);
                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getActivity(),mDatas[getAdapterPosition()],Toast.LENGTH_SHORT).show();
                        switch (getAdapterPosition()){
                            case 1:
                                SkinManager.getInstance().restoreDefaultTheme();
                                break;
                            case 2:
                                break;
                            case 3:
                                String s=XywUtil.getCertificateSHA1Fingerprint(getActivity());
                                tv.setText(s);
                                XywUtil.log(s);
//                                CD:67:D2:6F:12:E7:0C:E6:BB:67:DB:99:A3:10:18:89
//                                cd67d26f12e70ce6bb67db99a3101889
                                break;
                            case 4:
                                onSkinSetClick();
                                break;
                            case 5:
                                getActivity().finish();
                                break;
                        }
                    }
                });
            }
        }
    }

}
